package com.hospital.workstation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hospital.common.annotation.Log;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.bo.MedicationSummaryBo;
import com.hospital.workstation.domain.bo.PatientBaseInfoBo;
import com.hospital.workstation.domain.vo.PatientBaseInfoVo;
import com.hospital.workstation.domain.vo.StatisticsVo;
import com.hospital.workstation.service.IStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 统计管理
 *
 * @author whb
 * @date 2023-06-13
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/statistics")
public class StatisticsController {
    private final IStatisticsService iStatisticsService;

    /**
     * 我的药历
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @SaCheckPermission("workstation:statistics:my:list")
    @PostMapping("/my/list")
    public TableDataInfo<PatientBaseInfoVo> list(PatientBaseInfoBo bo, PageQuery pageQuery) {
        return iStatisticsService.queryMyMedicationPageList(bo, pageQuery);
    }

    /**
     * 删除我的药历
     *
     * @param medicalRecordNo 流水号
     * @return
     */
    @SaCheckPermission("workstation:statistics:my:remove")
    @Log(title = "删除我的药历", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @GetMapping("/my/remove/{medicalRecordNo}")
    public R<Void> removeMedicationByMedicalRecord(@PathVariable Integer medicalRecordNo) {
        try {
            iStatisticsService.removeMyMedicationRecord(medicalRecordNo);
            return R.ok("删除成功");
        } catch (Exception e) {
            return R.fail("删除失败");
        }
    }

    /**
     * 工作量统计
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @SaCheckPermission("workstation:statistics:workload")
    @PostMapping("/workload")
    public TableDataInfo<StatisticsVo> statisticsOfWorkload(MedicationSummaryBo bo, PageQuery pageQuery) {
        return iStatisticsService.queryStatisticsWorkload(bo, pageQuery);
    }

    /**
     * 下载工作量统计
     *
     * @param bo
     * @param response
     */
    @SaCheckPermission("workstation:statistics:workload:export")
    @PostMapping("/workload/export")
    public void export(MedicationSummaryBo bo, HttpServletResponse response) {
        List<StatisticsVo> listVo = iStatisticsService.queryStatisticsWorkloadList(bo);
        ExcelUtil.exportExcel(listVo, "工作量统计", StatisticsVo.class, response);
    }

    /**
     * 删除
     *
     * @param medicalRecordNo 病历号
     * @return
     */
    //@SaCheckPermission("workstation:statistics:workload:delete")
    @DeleteMapping("/del/{medicalRecordNo}")
    public R<Void> delete(@NotNull(message = "病历号不能为空")
                          @PathVariable Integer medicalRecordNo) {
        try {
            return iStatisticsService.del(medicalRecordNo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.fail("删除失败");
        }
    }
}
